package tianjunpei;

import java.util.*;

public class DoubleColorBall {
    private static final int RED_BALL_COUNT = 6;
    private static final int RED_BALL_RANGE = 33;
    private static final int BLUE_BALL_RANGE = 16;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);


        int[] userNumbers = getUserNumbers(scanner);
        System.out.println("您的投注号码：");
        printNumbers(userNumbers);


        int[] winningNumbers = generateWinningNumbers();
        System.out.println("本期中奖号码：");
        printNumbers(winningNumbers);


        int redMatches = countRedMatches(winningNumbers, userNumbers);
        boolean blueMatch = winningNumbers[6] == userNumbers[6];


        printResult(redMatches, blueMatch);
    }


    private static int[] generateWinningNumbers() {
        int[] numbers = new int[7];
        Random random = new Random();


        Set<Integer> redBalls = new HashSet<>();
        while (redBalls.size() < RED_BALL_COUNT) {
            redBalls.add(random.nextInt(RED_BALL_RANGE) + 1);
        }
        int index = 0;
        for (int ball : redBalls) {
            numbers[index++] = ball;
        }


        numbers[6] = random.nextInt(BLUE_BALL_RANGE) + 1;

        return numbers;
    }


    private static int[] getUserNumbers(Scanner scanner) {
        int[] numbers = new int[7];
        Set<Integer> redBalls = new HashSet<>();

        System.out.println("请输入6个红球号码（1-33，不能重复）：");
        for (int i = 0; i < RED_BALL_COUNT; i++) {
            while (true) {
                System.out.print("红球" + (i + 1) + ": ");
                int ball = scanner.nextInt();
                if (ball < 1 || ball > RED_BALL_RANGE) {
                    System.out.println("号码超出范围，请输入1-33之间的数字！");
                } else if (redBalls.contains(ball)) {
                    System.out.println("号码重复，请重新输入！");
                } else {
                    redBalls.add(ball);
                    numbers[i] = ball;
                    break;
                }
            }
        }

        System.out.println("请输入1个蓝球号码（1-16）：");
        while (true) {
            System.out.print("蓝球: ");
            int ball = scanner.nextInt();
            if (ball < 1 || ball > BLUE_BALL_RANGE) {
                System.out.println("号码超出范围，请输入1-16之间的数字！");
            } else {
                numbers[6] = ball;
                break;
            }
        }

        return numbers;
    }


    private static int countRedMatches(int[] winningNumbers, int[] userNumbers) {
        int matches = 0;
        for (int i = 0; i < RED_BALL_COUNT; i++) {
            for (int j = 0; j < RED_BALL_COUNT; j++) {
                if (winningNumbers[i] == userNumbers[j]) {
                    matches++;
                    break;
                }
            }
        }
        return matches;
    }


    private static void printResult(int redMatches, boolean blueMatch) {
        System.out.println("\n中奖结果：");
        System.out.println("红球命中数量: " + redMatches);
        System.out.println("蓝球是否命中: " + (blueMatch ? "是" : "否"));

        if (blueMatch) {
            switch (redMatches) {
                case 6 -> System.out.println("恭喜！您中了一等奖！");
                case 5 -> System.out.println("恭喜！您中了三等奖！");
                case 4 -> System.out.println("恭喜！您中了四等奖！");
                case 3 -> System.out.println("恭喜！您中了五等奖！");
                default -> System.out.println("恭喜！您中了六等奖！");
            }
        } else {
            switch (redMatches) {
                case 6 -> System.out.println("恭喜！您中了二等奖！");
                case 5 -> System.out.println("恭喜！您中了四等奖！");
                case 4 -> System.out.println("恭喜！您中了五等奖！");
                default -> System.out.println("很遗憾，您未中奖。");
            }
        }
    }


    private static void printNumbers(int[] numbers) {
        System.out.print("红球: ");
        for (int i = 0; i < RED_BALL_COUNT; i++) {
            System.out.print(numbers[i] + " ");
        }
        System.out.print("蓝球: " + numbers[6] + "\n");
    }
}